﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Text.RegularExpressions;

#endregion

namespace ScrumTable.Common.Excel.OpenXmlSdk.Extensions
{
    /// <summary>
    /// Provides a reference class which is used to point to a row index and
    /// a column name of a cell.
    /// </summary>
    public class CellReference
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        internal static readonly CellReference Empty = new CellReference();

        private static readonly Regex CellNameRegex = new Regex(@"^(\D+)(\d+)$", RegexOptions.Compiled);

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        internal int RowIndex { get; private set; }
        internal string ColumnName { get; private set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        private CellReference() { }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Parses the given reference and returns the corresponding instance.
        /// </summary>
        /// <param name="cellReference">Specifies the string to parse.</param>
        public static CellReference ParseReference(string cellReference)
        {
            CellReference cellRef = Empty;

            if (!string.IsNullOrEmpty(cellReference))
            {
                Match spanRange = CellNameRegex.Match(cellReference);
                int rowIdx;

                if (spanRange.Success && Int32.TryParse(spanRange.Groups[2].Value, out rowIdx))
                {
                    cellRef = new CellReference
                    {
                        ColumnName = spanRange.Groups[1].Value,
                        RowIndex = rowIdx
                    };
                }
            }
            return cellRef;
        }

        /// <summary>
        /// Creates a new CellReference instance with the corresponding information.
        /// </summary>
        /// <param name="columnName">Specifies the name of the column for the reference.</param>
        /// <param name="rowIndex">Specifies the index of the row for the reference.</param>
        public static CellReference ToReference(string columnName, int rowIndex)
        {
            PreCondition.AssertNotNullOrEmpty(columnName, "columnName");
            PreCondition.AssertTrue(rowIndex > 0, "", "rowIndex");

            return new CellReference
            {
                ColumnName = columnName,
                RowIndex = rowIndex
            };
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return ColumnName + RowIndex;
        }

        #endregion
    }
}